<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>供应商管理</el-breadcrumb-item>
      <el-breadcrumb-item>供应商管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button type="primary" class="btn" @click="adddialogVisible = true"
        >添加供应商</el-button
      >
      <el-row>
        <el-table
          :data="
            users.slice((currentPage - 1) * pagesize, currentPage * pagesize)
          "
          border
          style="width: 100%"
        >
          <el-table-column label="id" width="80">
            <template slot-scope="scope">
              <span>{{ scope.row.id }}</span>
            </template>
          </el-table-column>
          <el-table-column label="供应商名称">
            <template slot-scope="scope">
              <span>{{ scope.row.username }}</span>
            </template>
          </el-table-column>
          <el-table-column label="公司地址">
            <template slot-scope="scope">
              <span>{{ scope.row.place }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="性别">
      <template slot-scope="scope">
        <span text-align="center">{{ scope.row.sex}}</span>
      </template>
    </el-table-column> -->
          <el-table-column label="电话">
            <template slot-scope="scope">
              <span text-align="center">{{ scope.row.phone }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="年龄">
      <template slot-scope="scope">
        <span text-align="center">{{ scope.row.age}}</span>
      </template>
    </el-table-column> -->
          <el-table-column label="操作" width="200px">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="show(scope.row)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                icon="el-icon-delete"
                @click="remove(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="40"
      >
      </el-pagination>
    </el-card>
    <el-dialog
      title="修改供应商信息"
      :visible.sync="editdialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="closed"
    >
      <el-form
        :model="addForm"
        :rules="addrules"
        ref="addFormRef"
        label-width="70px"
      >
        <el-form-item label="id" prop="id">
          <el-input v-model="addForm.id" disabled></el-input>
        </el-form-item>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="place">
          <el-input v-model="addForm.place"></el-input>
        </el-form-item>
        <!-- <el-form-item label="性别" prop="sex">
    <el-input v-model="addForm.sex"></el-input>
  </el-form-item> -->
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <!-- <el-form-item label="年龄" prop="age">
    <el-input v-model="addForm.age"></el-input>
  </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editdialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="change">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="添加供应商"
      :visible.sync="adddialogVisible"
      width="50%"
      :before-close="handleClose"
      @close="closed"
    >
      <el-form
        :model="addForm1"
        :rules="addrules"
        ref="addFormRef"
        label-width="70px"
      >
        <!-- <el-form-item label="id" prop="id">
    <el-input v-model="addForm.id"></el-input>
  </el-form-item> -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm1.username"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="place">
          <el-input v-model="addForm1.place"></el-input>
        </el-form-item>
        <!-- <el-form-item label="性别" prop="sex">
    <el-input v-model="addForm.sex"></el-input>
  </el-form-item> -->
        <el-form-item label="电话" prop="phone">
          <el-input v-model="addForm1.phone"></el-input>
        </el-form-item>
        <!-- <el-form-item label="年龄" prop="age">
    <el-input v-model="addForm.age"></el-input>
  </el-form-item> -->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="adddialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addsupplier">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import axios from "axios";
import { getAll, upsert, removed } from "../../network/SupplierManage";
export default {
  data() {
    return {
      users: [
        //        {
        //  username:'张三科技公司',
        //  place:'福建省福州市福建师范大学',
        //  phone:'10299383883',
        //       },
        //          {
        //  username:'张三三科技股份有限公司',
        //  place:'福建省福州市福州大学',
        //  phone:'219383939333',
        //       },
        //          {
        //  username:'王五餐饮公司',
        //  place:'福建省厦门市厦门大学',
        //  phone:'2923888838',
        //       },
        //          {
        //  username:'李四',
        //  place:'福建省福州市闽江学院',
        //  phone:'10299383883',
        //       },
      ],
      query: "",
      currentPage: 1,
      pagesize: 5,
      adddialogVisible: false,
      editdialogVisible: false,
      state: "正常",
      addForm: {
        id: "",
        username: "",
        place: "",
        phone: "",
      },
      addForm1: {
        // id:'',
        username: "",
        place: "",
        phone: "",
      },
      addrules: {
        id: [{ required: true, message: "请输入id", trigger: "blur" }],
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        place: [{ required: true, message: "请输入地址", trigger: "blur" }],
        //   sex:[
        //   {required: true, message: '请输入性别', trigger: 'blur'}
        //  ],
        phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
        //   age:[
        //   {required: true, message: '请输入年龄', trigger: 'blur'}
        //  ]
      },
    };
  },
  created() {
    getAll()
      .then((res) => {
        var result = res.data.obj;
        this.users = result;
        console.log(res.data.obj);
      })
      .catch(function (error) {
        console.log(error);
      });
  },
  methods: {
    change() {
      upsert(this.addForm)
        .then((res) => {
          getAll()
            .then((res) => {
              var result = res.data.obj;
              this.users = result;
              console.log(res.data.obj);
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch(function (error) {
          console.log(error);
        });
      this.editdialogVisible = false;
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    getlist() {},
    handleSizeChange(val) {
      this.pagesize = val;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((_) => {
          done();
        })
        .catch((_) => {});
    },
    closed() {
      this.$refs.addFormRef.resetFields();
    },
    show(item) {
      this.editdialogVisible = true;
      this.addForm = JSON.parse(JSON.stringify(item));
    },
    remove(id) {
      removed({ id: id })
        .then(() => {
          getAll()
            .then((res) => {
              var result = res.data.obj;
              this.users = result;
              console.log(res.data.obj);
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch((error) => {
          console.log(error); //异常
        });
    },
    addsupplier() {
      upsert(this.addForm1)
        .then((res) => {
          getAll()
            .then((res) => {
              var result = res.data.obj;
              this.users = result;
              console.log(res.data.obj);
            })
            .catch(function (error) {
              console.log(error);
            });
        })
        .catch(function (error) {
          console.log(error);
        });
      this.adddialogVisible = false;
    },
  },
};
</script>


<style>
.el-breadcrumb {
  margin-bottom: 15px;
  font-size: 12px;
}
.btn {
  margin-left: 880px;
}
.el-table {
  margin-top: 15px;
}
.el-pagination {
  text-align: center;
}
</style>
